package web.vo;

import framework.pid.PidOption;
import framework.pid.PidUtil;
import lombok.Getter;
import org.springframework.beans.BeanUtils;
import system.entity.SysPermission;

import java.util.ArrayList;
import java.util.List;

public class PermissionOption extends SysPermission implements PidOption {

    @Override
    public String pid() {
        //return "-".equals(super.getPid()) ? "" : super.getPid();
        return super.getPid();
    }

    @Override
    public String id() {
        return super.getId();
    }

    @Override
    public Long order() {
        return Long.valueOf(super.getOrderNum());
    }

    @Getter
    private List<PidOption> children = new ArrayList<>();

    public PermissionOption(SysPermission permission) {
        BeanUtils.copyProperties(permission, this);
    }

    public static List<PermissionOption> asOptionList(List<SysPermission> permissions) {
        List<PermissionOption> list = new ArrayList<>();
        for (SysPermission permission : permissions) {
            list.add(new PermissionOption(permission));
        }
        return list;
    }

    public static List<PermissionOption> tree(List<SysPermission> permissions, boolean desc) {
        List<PermissionOption> list = new ArrayList<>();
        for (SysPermission permission : permissions) {
            list.add(new PermissionOption(permission));
        }
        List<PermissionOption> tree = PidUtil.tree(list, "-", desc);
        return tree;
    }
}
